package com.atguigu.demo.customer.controller;


import com.atguigu.demo.customer.bean.Customer;
import com.atguigu.demo.customer.service.CustomerService;
import com.atguigu.demo.customer.service.impl.CustomerServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController  //标识请求入口类
public class CustomerController {

    @Autowired
    CustomerService customerService;

    @GetMapping("/hello")
    public  String  helloCustomer(@RequestParam(value = "name" , defaultValue = "unknow" ,required = false ) String name , @RequestParam("age")Integer age){
        System.out.println("hello customer "+name +":"+age);
        return "hello customer  "+name +":"+age;

    }

    @GetMapping("/customer/{id}.html")
    public  String  getCustomer(@PathVariable("id") Long customerId   ){
        System.out.println("hello customer "+customerId   );
        Customer customer = customerService.getCustomerById(customerId);

        return "hello customer  ::"+customer;

    }

    @PostMapping("/customer")
    public String saveCustomer( @RequestBody Customer customer){
       // customerService.saveOrUpdate(customer);
        customerService.saveCustomer(customer);
       // System.out.println(customer);
        return "success";

    }
    @GetMapping("/customer")
    public List<Customer> getCustomerByName(@RequestParam("name") String customerName){
        List<Customer> customerList = customerService.list(new QueryWrapper<Customer>().eq("name", customerName));
        return customerList;
    }

}
